﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Management;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using Meqs.Logic.Implement;
using Meqs.Logic.Interface;
using Meqs.Entity;
using System.Text;

public partial class register : System.Web.UI.Page
{
    private Registration _registration;

    protected void Page_Load(object sender, EventArgs e)
    {
        //this.txtSn.Enabled = true;
        if (!IsPostBack)
        {
            labMsg.Visible = false;
            IList<Registration> registerList = Container<IRegistrationLogic>.Instance.FindAll();
            if (registerList == null || registerList.Count < 1)
                return;
            
            _registration = registerList[0];
            this.txtSchoolName.Text = _registration.SchoolName;
            this.txtProvince.Text = _registration.Provinces;
            this.txtCity.Text = _registration.City;
            this.txtPhone.Text = _registration.Deployment;
            UserSerial serial;
            try
            {
                serial = new UserSerial(_registration.Data);
            }
            catch (Exception)
            {
                return;
            }

            this.txtSn.Text = serial.SNum;
            if (serial.SNum.Trim().Length > 0)
                btnRegister.Text = "修改";
            //this.txtSn.Enabled = false;
        }
    }

    /// <summary>
    /// 取得网卡mac地址
    /// </summary>
    /// <returns></returns>
    protected static String GetMacAddress()
    {
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
        string MacAddress = "";

        foreach (ManagementObject mo in moc)
        {
            if ((bool)mo["IPEnabled"] == true)
            {
                MacAddress += mo["MacAddress"].ToString();
            }
            mo.Dispose();
        }
        return MacAddress;
    }
    /// <summary>
    /// 提交注册信息
    /// -1: 服务器异常
    /// 0: 完成注册（新增）
    /// 1: 完成注册（修改）
    /// 2: 序列号无效
    /// 3: 序列号使用次数达到上限
    /// 4: 数据库更新失败
    /// </summary>
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        int result = -1;

        Registration reg = new Registration();
        reg.SchoolName = txtSchoolName.Text;
        reg.Provinces = txtProvince.Text;
        reg.City = txtCity.Text;
        reg.Deployment = txtPhone.Text;

        UserSerial serial = new UserSerial();
        serial.SNum = txtSn.Text;
        serial.UserSecret = GetMacAddress();

        WebRegister.Registration r = new WebRegister.Registration();
        r.SchoolName = txtSchoolName.Text;
        r.Provinces = txtProvince.Text;
        r.City = txtCity.Text;
        r.Deployment = txtPhone.Text;
        r.Data = serial.GetArray();

        // 提交注册信息
        WebRegister.Service regService = new WebRegister.Service();
        result = regService.RegeditTeachEval(ref r);

        if (result != 0 && result != 1)
        {
            labMsg.Visible = true;
            if (result == -1)
                labMsg.Text = "服务器异常, 请稍后再试.";
            else if (result == 2)
                labMsg.Text = "序列号无效.";
            else if (result == 3)
                labMsg.Text = "序列号使用次数达到上限.";
            else if (result == 4)
                labMsg.Text = "数据库更新失败.";
            return;
        }

        Container<IRegistrationLogic>.Instance.DeleteAll();
        reg.Data = r.Data;
        result = Container<IRegistrationLogic>.Instance.Create(reg);
        if (result > 0)
        {
            GlobalApplication.Reged = true;

            //Response.Write("<script type='text/javascript'>setTimeout(\"window.location='login.aspx'\",5000)</script>");
            //构造JAVASCRIPT对象实体，
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language='javascript'>");
            sb.Append("showMassgeBox('提示', '注册成功');setTimeout(\"window.location='Default.aspx'\",5000);");
            sb.Append("</script>");
            //执行
            Page.ClientScript.RegisterStartupScript(this.GetType(), "RegOK", sb.ToString());
        }
        else
        {
            labMsg.Visible = true;
            labMsg.Text = "注册失败.";
        }
    }
}
